package com.gzx.plugin.tjzy.modular.file.controller;

import com.gzx.plugin.tjzy.modular.file.service.FileService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author zjt
 * @description
 * @date 2024/10/18 15:26
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/tjzy/file")
public class FileController {

    private final FileService fileService;

    @GetMapping("/createBucket/{bucketName}")
    public CommonResult<Void> createBucket(@PathVariable("bucketName") String bucketName) {
        fileService.createBucket(bucketName);
        return CommonResult.ok();
    }

    @PostMapping("/upload")
    public CommonResult<String> upload(MultipartFile file) throws IOException {
        return CommonResult.data(fileService.upload(file));
    }

    @PostMapping("/uploadNoTimeSuffix")
    public CommonResult<String> uploadNoTimeSuffix(MultipartFile file) throws IOException {
        return CommonResult.data(fileService.uploadNoTimeSuffix(file));
    }

    @GetMapping("/downloadWithBucket/{bucketName}")
    public void downloadWithBucket(@PathVariable String bucketName, @RequestParam String fileName, HttpServletResponse response) throws FileNotFoundException {
        fileService.downloadWithBucket(bucketName, fileName, response);
    }

    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) {
        fileService.download(fileName, response);
    }
}
